home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / basic / qbfaqr01.zip / CLINE.BAS < prev    next >
BASIC Source File  |  1992-08-10  |  9KB  |  225 lines

  1. '  Program CLINE.BAS
  2. '  Version 1.00
  3. '  Parses the command tail into an array holding all
  4. '  command line arguments.
  5. '  Written by: J. Derek Lyons.
  6. '  November 1991
  7. '  Released into the public domain to the extent of my ability to do so.
  8.  
  9. DECLARE SUB ParCline (Arg$(), MaxArg%, Res%)
  10.  
  11. DEFINT A-Z
  12. OPTION BASE 0
  13.  
  14. DIM Arg$(5)             'Array to hold the arguments
  15. MaxArg% = 5             'Maximum number of arguments
  16. '
  17. '  To demonstrate CLINE, simply compile this program inside Quick Basic
  18. '  or from the command line.
  19. '
  20. '  Many more options are discussed in CLINE.DOC
  21. '
  22. CLS
  23. CALL ParCline(Arg$(), MaxArg%, Res%)
  24. FOR x = 1 TO 5: PRINT Arg$(x): NEXT x
  25. IF Res% = -1 THEN PRINT "Too Many Arguments"
  26. IF Res% = 0 THEN PRINT "Sucessful Processing"
  27. IF Res% = 1 THEN PRINT "No Arguments Found"
  28. END
  29.  
  30. SUB ParCline (Arg$(), MaxArg%, Res%)
  31. '  Inputs
  32. '     MaxArg%   Maximum number of arguments
  33. '     Arg$()    Empty array to hold the arguments
  34. '               To work properly should be DIMed as Arg$(MaxArg%)
  35. '  Outputs
  36. '     Res%      Result of subroutine
  37. '               -1 = Too many arguments
  38. '                0 = Sucessful processing
  39. '                1 = No arguments found
  40. '     Arg$()    Array holding the arguments
  41. '
  42.                         'numarg and argpos must be initialized
  43.                         'because QB initializes them as 0
  44. NumArg = 1              'Because there is no leading space for the
  45.                         'first argument we must add 1 to the total
  46.                         'number of space to find the total number
  47.                         'of arguments
  48. ArgPos = 1              'The first position in the array
  49.  
  50. Cline$ = LTRIM$(RTRIM$(COMMAND$))
  51.                         'Get the command line and trim all the spaces
  52. Clen = LEN(Cline$)      'Get the length of the command line
  53.  
  54. IF Clen = 0 THEN        'There are no arguments so there is no reason
  55.                         'to continue processing the command line
  56.    Res% = 1
  57.    EXIT SUB
  58. END IF
  59.  
  60. FOR Scount = 1 TO Clen  'Get the number of arguments
  61.    IF MID$(Cline$, Scount, 1) = " " THEN NumArg = NumArg + 1
  62.                         'Each time a space is found in the command line
  63.                         'the number of arguments is incremented
  64. NEXT Scount
  65.  
  66. IF NumArg > MaxArg% THEN
  67.                         'So we don't crash the program by trying to
  68.                         'write past the end of the array
  69.    Res% = -1
  70.    EXIT SUB
  71. END IF
  72.  
  73. FOR wcount = 1 TO Clen
  74.    IF MID$(Cline$, wcount, 1) <> " " THEN
  75.       Arg$(ArgPos) = Arg$(ArgPos) + MID$(Cline$, wcount, 1)
  76.                         'If a character is found, then add it to the
  77.                         'current string
  78.    ELSEIF MID$(Cline$, wcount, 1) = " " THEN
  79.       ArgPos = ArgPos + 1
  80.                         'If a space is found, start processing the
  81.                         'next string
  82.    END IF
  83. NEXT wcount
  84.  
  85. END SUB
  86. '     CLINE.BAS
  87. '     Version 1.00
  88. '     Mountain Bay Software
  89. '     James Derek Lyons
  90. '
  91. '     A subroutine to parse the command line for
  92. '     QUICK BASIC programs.
  93.  
  94. 'CLINE.BAS is hereby released into the public domain to the extent
  95. 'of my legal rights to do so.
  96. 'The author makes no warranty as to the fitness of this code for any
  97. 'given application.  The responsibility for determining fitness of
  98. 'use and for any damages caused lies with the user.
  99.  
  100. 'CLINE.BAS has been tested using MSDOS V3.3 and Quick Basic V4.5.
  101. 'QUICK BASIC and MSDOS are registered trademarks of the Microsoft
  102. 'Corporation.
  103.  
  104. 'INDEX
  105.  
  106. '1.      Overview
  107. '2.      Program Logic.
  108. '2A.     The Parsing Algorithm
  109. '3A.     Error Handling.
  110.  
  111. '1. OVERVIEW
  112.  
  113. '     One of the most useful functions of MSDOS is the ability to
  114. 'use a 'command tail'.  That is to say, a set of variables which
  115. 'can be read by a program at run-time and used to modify it's
  116. 'operation.
  117. '     In QUICK BASIC the COMMAND$ function can be used to read the
  118. 'command tail into your program.  However, this function returns the
  119. 'entire command tail as a single string.  Unless you are using only
  120. 'one run-time option, this is fairly useless.
  121. '     CLINE offers the QUICK BASIC programmer a method of importing
  122. 'this command tail and parsing it into useful string variables.
  123.  
  124. '2. PROGRAM LOGIC
  125.  
  126. '     The algorithm used by CLINE is fairly simple.  The requirements
  127. 'for using this subroutine are deliberately held to a minimum.
  128. '     Three variables are required to use the subroutine.  Two must
  129. 'be declared in advance.
  130.  
  131. '     These variables are:
  132. '     MaxArg%, which is the maximum number of arguments expected.
  133. '     Arg$(),  which is a string array to hold the returned, parsed,
  134. '              arguments.
  135. '     Res%,    which is a variable to hold the result flag for the
  136. '              subroutine.
  137.  
  138. '     The following assumptions apply these variables;
  139.  
  140. '     MaxArg% is the total number of arguments that the user can
  141. 'legally use when loading the program.  As will be shown later each
  142. 'argument is assumed to be separated by a space.  Thus "/FILE DUMMY
  143. 'would be counted as two arguments.  "/FILE:DUMMY and "-AJ2" would
  144. 'both be considered to be one argument.
  145.  
  146. '     ARG$() is a string array to hold the arguments when they are
  147. 'parsed.  To prevent programs from bombing, ARG$() is best
  148. 'dimensioned by using DIM ARG$(MaxArg%).
  149.  
  150. '     Res% is an integer flag that returns the result of the parsing
  151. 'process.  These results are defined as follows;
  152.  
  153. '     -1   indicates that too many arguments were found.  Processing
  154. '           is halted and control returned to the calling program.
  155. '      0   indicates that processing was successful and the parsed
  156. '          arguments will be found in ARG$().
  157. '      1   Indicates that no command line was found.  Processing is
  158. '          halted and control is returned to the calling program.
  159.  
  160. '     No error handling is performed by CLINE other than the setting
  161. 'of Res% to the appropriate value.
  162.  
  163. '2A. The Parsing Algorithm
  164.  
  165. '     The command tail retrieved by COMMAND$ is processed as
  166. 'follows;
  167. '     First all leading and trailing spaces are removed using the
  168. 'LTRIM$() and RTRIM$() functions.  Since the algorithm determines
  169. 'the number of arguments by counting the number of spaces, any
  170. 'extraneous ones at the beggining and end must be removed.
  171. '     Because there is no leading space for the first argument, the
  172. 'NUMARG and ARGPOS() variables are initialized to 1.
  173. '     The length of the command tail is then determined.  If no tail
  174. 'is found, processing is returned to the calling program.  A flag
  175. 'is set to inform the calling program that no command line options
  176. 'were found.
  177. '     Each position in the string is then examined using the
  178. 'MIDSTRING$() function.  Each time a space is encountered, the
  179. 'argument count is increased by one.
  180. '     The total number of arguments found by this statement is then
  181. 'compared to the maximum allowable number.  If the number found
  182. 'exceeds the number allowed, processing is halted and control
  183. 'returned to the calling program.  The programmer must provide code
  184. 'to handle this error and inform the user of the failure.
  185. '     Each position in the string is then examined.  If a non-space
  186. 'character is encountered, the character is added to the current
  187. 'string.  If a space is encountered, the string number is
  188. 'incremented by one and processing continues with the next
  189. 'character.
  190.  
  191. '     Hence the string /FILE DUMMY /A -AQD2 /OUTFILE:TEST would
  192. 'parse as follows;
  193.  
  194. '     String #1     /FILE
  195. '     String #2     DUMMY
  196. '     String #3     /A
  197. '     String #4     -AQD2
  198. '     String #5     /OUTFILE:TEST
  199.  
  200. '     Note that because of the way COMMAND$ functions, all
  201. 'alphabetic characters will be in upper case.
  202.  
  203. '     Control is then returned to the calling program.
  204.  
  205. '3A. ERROR HANDLING
  206.  
  207. '     Other than errors relating to the number of arguments, and the
  208. 'lack of a command tail, no native error handling is provided.
  209.  
  210. '     If too many arguments are encountered, the programmer must
  211. 'provide routines to inform the user of the syntax error and recover
  212. 'from the error condition.
  213.  
  214. '     If no arguments are provided then a flag is set to inform the
  215. 'calling program.  The programmer must provide code for his program
  216. 'to respond approprietly.
  217.  
  218. '     It is suggested that if too many, or no, command line
  219. 'arguments are found, that any defaults be loaded and the user
  220. 'informed.
  221.  
  222. '     If this code is used in a command line utility, (a program
  223. 'that is run only from the command line), that the program inform
  224. 'the user and exit gracefully.
  225.